layui.use(['form', 'layer', 'table', 'laydate', 'func', 'ax'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var func = layui.func;
    var $ax = layui.ax;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    /**
     * 初始化表格
     */
    var BrankBannerTwo = {
        tableId: "BrankBannerTwoTable" //表格id
    };

    /**
     * 初始化表格的列
     */
    BrankBannerTwo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'maxTitle', align: "center", sort: true, title: '大标题'},
            {field: 'minTitle', align: "center", sort: true, title: '小标题'},
            {field: 'content', align: "center", sort: true, title: '内容'},
            {field: 'isShow', title: '是否显示', align: 'center', width: 120,
                templet: function (object) {
                    if(object.isShow == 0){
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" checked>';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '">';
                    }
                }
            },
            {field: 'updateDate', align: "center", sort: true, title: '更新时间'},
            {field: 'userName', align: "center", sort: true, title: '更新者'},
            {align: 'center', toolbar: '#tableBar', title: "操作", minWidth: 80}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BrankBannerTwo.tableId,
        url: Feng.ctxPath + '/rw/brankBannerTwo/list',
        page: true,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        height: "full-98",
        cellMinWidth: 100,
        cols: BrankBannerTwo.initColumn()
    });

    /**
     * 弹出添加对话框
     */
    BrankBannerTwo.openAdd = function () {
        func.open({
            title: "添加",
            maxmin: true,
            height: 700,
            content: Feng.ctxPath + '/rw/brankBannerTwo/addOrEdit',
            tableId: BrankBannerTwo.tableId
        });
    };

    /**
     * 弹出编辑对话框
     */
    BrankBannerTwo.onEdit = function (data) {
        func.open({
            title: "编辑",
            maxmin: true,
            height: 700,
            content: Feng.ctxPath + '/rw/brankBannerTwo/addOrEdit?brankBannerTwoId=' + data.id,
            tableId: BrankBannerTwo.tableId
        });
    };

    /**
     * 点击删除按钮
     *
     * @param data 点击按钮时候的行数据
     */
    BrankBannerTwo.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/rw/brankBannerTwo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BrankBannerTwo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("brankBannerTwoId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除该记录?", operation);
    };

    //监听是否显示开关
    form.on('switch(l_isShowSwitch)', function(data){
        var isShow = 1;
        if(data.elem.checked){
            isShow = 0;
        }

        var brankBannerTwoId = data.value; //编号

        //更新状态
        var ajax = new $ax(Feng.ctxPath + "/rw/brankBannerTwo/updateIsShow", function (data) {
            Feng.success("修改成功!");
            table.reload(BrankBannerTwo.tableId);
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set({
            isShow: isShow,
            brankBannerTwoId: brankBannerTwoId
        });
        ajax.start();
    });

    // 添加按钮点击事件
    $("#btnAdd").click(function(){
        BrankBannerTwo.openAdd();
    });

    // 工具条点击事件
    table.on('tool(' + BrankBannerTwo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BrankBannerTwo.onEdit(data);
        } else if(layEvent === 'delete'){
            BrankBannerTwo.onDelete(data);
        }
    });

});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});